package com.alcatellucent.ldap.util;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.alcatellucent.R;
import com.alcatellucent.vo.CompanyVO;
import com.alcatellucent.vo.UserVO;

public class LdapCompanyAdapter extends BaseExpandableListAdapter {

	private Context context;
	private ArrayList<CompanyVO> companyList;

	public LdapCompanyAdapter(Context applicationContext,
			ArrayList<CompanyVO> companyList) {
		this.context = applicationContext;
		this.companyList = companyList;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public ArrayList<CompanyVO> getCompanyList() {
		return companyList;
	}

	public void setCompanyList(ArrayList<CompanyVO> companyList) {
		this.companyList = companyList;
	}

	public Object getChild(int groupPosition, int childPosition) {
		ArrayList<UserVO> childList = companyList.get(groupPosition)
				.getUserList();
		return childList.get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		UserVO userInfo = (UserVO) getChild(groupPosition, childPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.companychild, null);
		}
		if (userInfo != null) {

			TextView name = (TextView) convertView.findViewById(R.id.textView3);
			TextView extension = (TextView) convertView
					.findViewById(R.id.textView6);
			TextView mail = (TextView) convertView.findViewById(R.id.textView7);

			name.setText(userInfo.getName());
			extension.setText(userInfo.getExtension());
			mail.setText(userInfo.getMail());

		}
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {

		ArrayList<UserVO> childList = companyList.get(groupPosition)
				.getUserList();
		if (childList != null)
			return childList.size();
		else {
			return 0;
		}
	}

	public Object getGroup(int groupPosition) {
		return companyList.get(groupPosition);
	}

	public int getGroupCount() {
		return companyList.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		CompanyVO company = (CompanyVO) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.companygroup, null);
		}

		if (company != null) {
			TextView tv = (TextView) convertView.findViewById(R.id.textView1);
			tv.setText(company.getCompany());
			return convertView;
		}

		return null;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
